import form from '@/views/dashboard/loan/form';
import { getTemplateFields } from '@/api/flow/template';

export default {
  mixins: [form],

  data() {
    return {
      fieldsPromise: null,

      // forms
      formFields: [
        // 借款金额
        {
          label: 'amount',
          value: '',
          link: 'borrow_money',
          callback: (data) => data.toString(),
          field: {}
        },
        // 手续费
        {
          label: 'charges_free',
          value: '',
          link: 'poundage',
          callback: (data) => data.toString(),
          field: {}
        },
        // 利息
        {
          label: 'interest_ratio',
          value: '',
          link: 'interest',
          callback: (data) => (data * 100).toString(),
          field: {}
        },
        // 滞纳金
        {
          label: 'late_fee_ratio',
          value: '',
          link: 'delay_payment',
          callback: (data) => (data * 100).toString(),
          field: {}
        },
        // 还款方式
        {
          label: 'repay_methods',
          value: '',
          link: 'repay_way',
          callback: (data) => {
            const repayments = [];
            data.forEach((item) => {
              repayments.push(
                `${item.ratio * 100}% repayment after ${item.day} days`
              );
            });

            return repayments.toString();
          },
          field: {}
        },
        // 卡号
        {
          label: 'card',
          value: '',
          link: 'credit_account',
          callback: (data) => (data.accountNo),
          field: {}
        },

        // 机构
        {
          label: 'card',
          value: '',
          link: 'credit_card',
          callback: (data) => (data.name),
          field: {}
        }
      ]
    };
  },

  created() {
    this.fieldsPromise = this.getTemplateFields();
  },

  methods: {
    // 获取表单提交数据
    getFieldsDatas(formFields) {
      const datas = formFields
        .map(({
          field: f, value, label, callback
        }) => ({
            field: f.id,
            field_value: { value: callback(this.risk[label]) }
          }));

      return datas;
    },

    // 获取表单信息
    getTemplateFields() {
      return new Promise((resolve, reject) => {
        this.getLimits()
          .then(() => {
            this.getWorkTask()
              .then(() => {
                getTemplateFields(this.tid)
                  .then((response) => {
                    this.generateFormFields(response, this.formFields);
                    // this.generateFormFields(response, this.loanFormFields);
                    resolve(response);
                  })
                  .catch((err) => reject(err));
              })
              .catch((err) => reject(err));
          })
          .catch((err) => reject(err));
      });
    }
  }
};
